package com.hyts.mvc.loader.item;
import com.hyts.mvc.bean.MvcConfigBean;
import com.hyts.mvc.config.MvcKey;
import com.hyts.mvc.config.MvcValue;
import com.hyts.mvc.config.ViewType;
import com.hyts.mvc.configure.WebMvcConfigurer;
import com.hyts.mvc.loader.WebMvcConfigLoader;
import com.hyts.mvc.servlet.DefaultWebMvcConfiguration;
import com.hyts.properties.manager.PropertyManager;
import org.apache.commons.lang.StringUtils;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.handler.config
 * author:Libo/Alex
 * create-date:2019-06-21 11:16
 * email:liboware@gmail.com
 * description:此类主要用于: 通过注解读取相关的配置服务-主要目的为-手动启动服务-此servlet服务采用的为jar包启动服务
 */
public class WebMvcPropertiesConfigLoader extends WebMvcConfigLoader<String> {

    /**
     * 读取配置mvc总体核心转发配置
     * @param param
     * @return
     */
    @Override
    public MvcConfigBean load(String param, MvcConfigBean configBean) {
        //获取propertisManager管理对象
        PropertyManager propertyManager = PropertyManager.getInstance().
                settingDefaultConfigurationName(StringUtils.defaultIfBlank(param, MvcValue.DEFAULT_CONFIG_FILE_NAME));
        //操作对象判断是否获取到对象
        if(propertyManager == null){
            configBean.setPrefixPath(MvcValue.DEFAULT_VIEW_PREFIX_PATH)
                    .setSubfixPath(MvcValue.DEFAULT_VIEW_SUBFIX_PATH)
                    .setScanPackage(new String[]{""})
                    .setUrlPath(StringUtils.defaultIfBlank(param, MvcValue.DEFAULT_VIEW_SERVLET_URL_PATH))
                    .setViewType(ViewType.THYMELEAF);
            return configBean;
        }
        //返回MvcConfigBean对象
        return  configBean.setPrefixPath(StringUtils.defaultIfBlank(propertyManager.getValue2Text(MvcKey.MVC_VIEW_PREFIX_PATH), MvcValue.DEFAULT_VIEW_PREFIX_PATH))
                .setSubfixPath(StringUtils.defaultIfBlank(propertyManager.getValue2Text(MvcKey.MVC_VIEW_SUBFIX_PATH), MvcValue.DEFAULT_VIEW_SUBFIX_PATH))
                .setScanPackage(new String[]{StringUtils.defaultIfBlank(propertyManager.getValue2Text(MvcKey.SCAN_MVC_PACKAGE_KEY),
                        MvcValue.DEFAULT_PACKAGE_NAME.toString())})
                .setUrlPath(StringUtils.defaultIfBlank(propertyManager.getValue2Text(MvcKey.MVC_SERVLET_URL_PATH_KEY), MvcValue.DEFAULT_VIEW_SERVLET_URL_PATH))
                .setViewType(ViewType.THYMELEAF);
    }

    @Override
    public int order() {
        return 3;
    }

    public static void main(String[] args){
        DefaultWebMvcConfiguration defaultWebMvcConfiguration = new DefaultWebMvcConfiguration();
        System.out.println(WebMvcConfigurer.class.isAssignableFrom(defaultWebMvcConfiguration.getClass()));
    }
}
